/**
 * =================================================================
 * 版权所有 2011-2013 深圳市泰海网络科技服务有限公司，并保留所有权利
 * -----------------------------------------------------------------
 * 这不是一个自由软件！您不能在任何未经允许的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布
 * =================================================================
 */
package com.imoney.payment.web.utils;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.imoney.framework.base.utils.StringUtils;
import com.imoney.payment.authc.domain.Auth;

/**
 * 类说明：<br>
 * 
 * <p>
 * 详细描述：<br>
 * 
 * </p>
 * 
 * <pre>
 * ——————————————————————————————————————————————————————————————————
 * |		修改人		|		修改时间			|		修改原因
 * ——————————————————————————————————————————————————————————————————
 * |	zengxx 曾宪新	|		2013-5-27		|	
 * ——————————————————————————————————————————————————————————————————
 * </pre>
 * 
 * @author zengxx 曾宪新(Xavier.zeng)
 * 
 *         CreateDate: 2013-5-27
 */
public class MenuUtil {
	
	private static List<List<Auth>> items = new ArrayList<List<Auth>>();
	

	public static void getMenuItem(int type, int itemIndex, int subIndex) {
		if (null != items && !items.isEmpty()){
			List<Auth> itemList = items.get(type);
			if (null != itemList && !itemList.isEmpty()) {
				for (Auth item : itemList){
					if (StringUtils.isNotEmpty(item.getStyle())){
						item.setStyle("");
						List<Auth> subList = item.getSubItems();
						for (Auth sub : subList){
							sub.setStyle("");
							break;
						}
						break;
					}
				}
				
				Auth item = itemList.get(itemIndex);
				if (null != item){
					item.setStyle("Current");
				}
				List<Auth> subList = item.getSubItems();
				if (subList != item && !subList.isEmpty()){
					Auth sub = subList.get(subIndex);
					sub.setStyle("Current");
				}
			}
			
			RequestAttributes ra = RequestContextHolder.getRequestAttributes();  
			HttpServletRequest request = ((ServletRequestAttributes)ra).getRequest();  
			request.getSession().setAttribute("items", itemList);  
		}
	}
	
	public static void setItems(List<Auth> authList){
		List<List<Auth>> items = new ArrayList<List<Auth>>();
		List<Auth> items1 = new ArrayList<Auth>();
		List<Auth> items2 = new ArrayList<Auth>();
		items.add(items1);
		items.add(items2);
		List<Auth> subItems = null;
		for (Auth auth : authList){
			if (1 == auth.getLevel()){
				subItems = new ArrayList<Auth>();
				auth.setSubItems(subItems);
				if ("0".equals(auth.getMenuType())){
					items1.add(auth);
					items2.add(auth);
				}
				else if ("1".equals(auth.getMenuType())){ 
					items1.add(auth);
				}
				else if ("2".equals(auth.getMenuType())){ 
					items2.add(auth);
				}
			}
			else if (2 == auth.getLevel()){
				subItems.add(auth);
			}
		}
		
		MenuUtil.items = items;
	}

}
